package cn.stronglink.asset.module.assetmanage.cabinet.service;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.IdWorker;

import cn.stronglink.asset.model.AssetModel;
import cn.stronglink.asset.model.AssetType;
import cn.stronglink.asset.model.CabinetExt;
import cn.stronglink.asset.model.ItAsset;
import cn.stronglink.asset.model.ItAssetExtPosition;
import cn.stronglink.asset.model.TRoom;
import cn.stronglink.asset.module.assetmanage.cabinet.mapper.CabinetExtMapper;
import cn.stronglink.asset.module.assetmanage.cabinet.vo.CabinetExtVo;
import cn.stronglink.asset.module.assetmanage.itasset.mapper.ItassetExtPositionMapper;
import cn.stronglink.asset.module.assetmanage.itasset.mapper.ItassetMapper;
import cn.stronglink.asset.module.assetmanage.itasset.vo.ItAssetVo;
import cn.stronglink.asset.module.basicdata.tRoom.mapper.TRoomMapper;
import cn.stronglink.suite.core.util.WebUtil;

@Service
@Transactional(rollbackFor=Exception.class) 
public class CabinetExtService {

	@Autowired
	private CabinetExtMapper cabinetExtMapper;
	@Autowired
	private ItassetMapper itassetMapper;
	@Autowired
	private ItassetExtPositionMapper itassetExtPositionMapper;
	@Autowired
	private TRoomMapper tRoomMapper;
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<CabinetExtVo> qryCabinetList(Map<String, Object> params) {
		if(params.get("roomId").toString() == null||"".equals(params.get("roomId").toString())) {
			List<TRoom> roomList = tRoomMapper.queryRoomList();
			if(roomList!=null&&roomList.size()>0) {
				params.put("roomId", roomList.get(0).getId());
			}
		}
		List<CabinetExtVo> list =new ArrayList<CabinetExtVo>();
		if("1".equals(params.get("type").toString())) {
			list = cabinetExtMapper.qryCabinetList(params);
		}else {
			list = cabinetExtMapper.qryCabinetListOfGroup(params);
		}
		if(list!=null&&list.size()>0) {
			for(CabinetExtVo vo :list) {
				int uUsed = itassetMapper.qryisUsedUByRackId(vo.getAssetId());
				int unUsed =vo.getuHeight()-uUsed;
				 DecimalFormat df = new DecimalFormat("0.00");//格式化小数    
			     String num = df.format((float)unUsed/vo.getuHeight());//返回的是String类型    
			     NumberFormat nt = NumberFormat.getPercentInstance();
			     //设置百分数精确度2即保留两位小数
			     nt.setMinimumFractionDigits(0);
			     System.out.println("ddd==="+nt.format(Double.valueOf(num)));  
			     vo.setOccupancy(nt.format(Double.valueOf(num)));
			}
		}
		return list;
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Page<CabinetExtVo> qryList(Page<CabinetExtVo> page, Map<String, Object> params) {
		// TODO Auto-generated method stub
		List<CabinetExtVo> list = cabinetExtMapper.qryList(page,params);
		page.setRecords(list);
		return page;
	}

	public void edit(CabinetExtVo cabinetExt) {
		ItAsset asset = new ItAsset();
		asset.setId(cabinetExt.getId());
		asset.setAssetName(cabinetExt.getAssetName());
		asset.setTypeId(cabinetExt.getRackTypeId());   //设置类型id
		asset.setBrandId(cabinetExt.getRackBrandId());
		asset.setModelId(cabinetExt.getRackModelId());
		asset.setUpdateTime(new Date());
		asset.setUpdateBy(WebUtil.getCurrentUser());
		itassetMapper.updateItAssetById(asset);
		
		cabinetExtMapper.remove(cabinetExt.getId());	
		itassetExtPositionMapper.removeAsset(cabinetExt.getId());
	
		cabinetExt.setId(IdWorker.getId());
		cabinetExt.setAssetId(asset.getId());
		cabinetExt.setCreateBy(WebUtil.getCurrentUser());
		cabinetExt.setCreateTime(new Date());
		// 插入机柜扩展表
		cabinetExtMapper.createCabinetExt(cabinetExt);
		
		ItAssetExtPosition itAssetExtPosition = new ItAssetExtPosition();
		itAssetExtPosition.setId(IdWorker.getId());
		itAssetExtPosition.setRoomId(cabinetExt.getRoomId());
		itAssetExtPosition.setAssetId(asset.getId());
		itAssetExtPosition.setX(cabinetExt.getX());
		itAssetExtPosition.setY(cabinetExt.getY());
		itAssetExtPosition.setZ(cabinetExt.getZ());
		itAssetExtPosition.setRotation(cabinetExt.getRotation());
		itAssetExtPosition.setCreateBy(WebUtil.getCurrentUser());
		itAssetExtPosition.setCreateTime(new Date());
		itassetExtPositionMapper.insertItAssetExtPosition(itAssetExtPosition);
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<CabinetExtVo> qryById(Map<String, Object> params) {
		List<CabinetExtVo> cabinetList =new ArrayList<CabinetExtVo>();
		if("1".equals(params.get("type").toString())) {
			cabinetList = cabinetExtMapper.qryById(Long.parseLong(params.get("id").toString()));
		}else {
			cabinetList = cabinetExtMapper.qryCabinetListByGroupId(Long.parseLong(params.get("id").toString()));
		}
		if(cabinetList!=null&&cabinetList.size()>0) {
			for(CabinetExtVo vo:cabinetList) {
				int uUsed = itassetMapper.qryisUsedUByRackId(vo.getAssetId());
				int unUsed =vo.getuHeight()-uUsed;
				vo.setIsUserd(uUsed);
				vo.setUnUserd(unUsed);
				List<ItAssetVo> assetList = itassetMapper.getAssetListByRackId(vo.getAssetId());
				vo.setAssetList(assetList);
			}			
		}
		return cabinetList;
	}

	public void remove(Long id) {
		int count = cabinetExtMapper.queryCabinetAssetNum(id);
		if(count==0) {
			itassetMapper.removeItAsset(id);
			cabinetExtMapper.remove(id);	
			itassetExtPositionMapper.removeAsset(id);
		}
	}

	public void create(CabinetExtVo cabinetExt) {
		ItAsset itAsset = new ItAsset();
		itAsset.setId(IdWorker.getId());
		itAsset.setAssetName(cabinetExt.getAssetName());
		itAsset.setCreateBy(WebUtil.getCurrentUser());
		itAsset.setCreateTime(new Date());
		itAsset.setTypeId(cabinetExt.getRackTypeId());   //设置类型id
		itAsset.setBrandId(cabinetExt.getRackBrandId());
		itAsset.setModelId(cabinetExt.getRackModelId());
		// 插入机柜表
		itassetMapper.insertItAsset(itAsset);
		
		
		cabinetExt.setId(IdWorker.getId());
		cabinetExt.setAssetId(itAsset.getId());
		cabinetExt.setCreateBy(WebUtil.getCurrentUser());
		cabinetExt.setCreateTime(new Date());
		// 插入机柜扩展表
		cabinetExtMapper.createCabinetExt(cabinetExt);
		
		ItAssetExtPosition itAssetExtPosition = new ItAssetExtPosition();
		itAssetExtPosition.setId(IdWorker.getId());
		itAssetExtPosition.setRoomId(cabinetExt.getRoomId());
		itAssetExtPosition.setAssetId(itAsset.getId());
		itAssetExtPosition.setX(cabinetExt.getX());
		itAssetExtPosition.setY(cabinetExt.getY());
		itAssetExtPosition.setZ(cabinetExt.getZ());
		itAssetExtPosition.setRotation(cabinetExt.getRotation());
		itAssetExtPosition.setCreateBy(WebUtil.getCurrentUser());
		itAssetExtPosition.setCreateTime(new Date());
		cabinetExtMapper.createCabinetExtPosition(itAssetExtPosition);
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<AssetType> qryRackTypeList() {
		List<AssetType> data = cabinetExtMapper.qryRackTypeList();
		return data;
	}
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<AssetModel> qryRackModelList(Map<String, Object> params) {
		List<AssetModel> list = cabinetExtMapper.qryRackModelList(params);
		return list;
	}

	public List<CabinetExt>  qryCabinetExtList(Map<String, Object> params) {
		return cabinetExtMapper.qryCabinetExtList(params);
	}

	public Page<HashMap<String, Object>> searchTemperature(Page<HashMap<String, Object>> page,
			Map<String, Object> params) {
		List<HashMap<String, Object>> list = cabinetExtMapper.searchTemperature(page,params);
		page.setRecords(list);
		return page;
	}

}
